<?php
// 本类由系统自动生成，仅供测试用途
class CapitalAction extends BaseLoginAction {
   function __construct() {
       parent::__construct();
       
   }
    public function index(){
        $this->redirect("lists");
    }
    
    public function lists(){
        $sort=D("Capital");
        $array=array();
        $where=array(
            C("DB_PREFIX")."capital.mid"=>  $this->uid,
            );
        //账户筛选
        if(isset($_GET['smid'])&&  intval($_GET['smid'])>=0){
            $smid=  intval($_GET['smid']);
        }else{
            $smid='';
        }
        $this->assign("smid",$smid);
        if($smid!==''){
            $where[C("DB_PREFIX")."capital.smid"]=$smid;
            $array['smid']=$smid;
        }
        //操作类型筛选
        if(isset($_GET['place'])&&  intval($_GET['place'])>0){
            $place=  intval($_GET['place']);
        }else{
            $place=0;
        }
        $this->assign("place",$place);
        if($place){
            $where[C("DB_PREFIX")."capital.place"]=$place;
            $array['place']=$place;
        }
        //转入账户筛选
        if(isset($_GET['in_id'])&&  intval($_GET['in_id'])>0){
            $in_id=  intval($_GET['in_id']);
        }else{
            $in_id='';
        }
        $this->assign("in_id",$in_id);
        if($in_id!==''){
            $where[C("DB_PREFIX")."capital.in_sid"]=$in_id;
            $array['in_id']=$in_id;
        }
        
        
        $this->assign("array",$array);
        $page=  page(10, $sort, $where,$array);
        $this->assign("page",$page);
        $list=$sort
                ->where($where)
                ->join(C("DB_PREFIX")."capital_sort in_sort ON in_sort.sid=".C("DB_PREFIX")."capital.in_sid")
                ->join(C("DB_PREFIX")."capital_sort exp_sort ON exp_sort.sid=".C("DB_PREFIX")."capital.exp_sid")
                ->join(C("DB_PREFIX")."capital_sort_member smember ON smember.id=".C("DB_PREFIX")."capital.smid")
                ->field(C("DB_PREFIX")."capital.*,
                    exp_sort.name as exp_name,exp_sort.place as exp_place,
                    in_sort.name as in_name,in_sort.place as in_place,
                    smember.name as sm_name
                    ")
                ->order("id desc")
                ->limit("{$page['start']},{$page['number']}")
                ->select();
       // echo $sort->getLastSql();
        $this->assign("list",$list);
        
        //查询账户列表
        $member=D("CapitalSortMember");
        $where=Array(
            "mid"=>  $this->uid
        );
        $member_list=$member->where($where)->select();
        $this->assign("member",$member_list);
        //账户分类列表
        $sort=D("CapitalSort");
        $where=Array(
            "mid"=>  $this->uid,
            "fsid"=>array("neq",0)
        );
        $sort_list=$sort->where($where)->select();
        
        $this->assign("sort",$sort_list);
        
        
        
        $this->display();
    }
    
   
    public function add(){
        if(isset($_GET['place'])&&  intval($_GET['place'])>0){
            $place=intval($_GET['place']);
        }else{
            $place=1;
        }
        if(isset($_GET['type'])&&  intval($_GET['type'])>0){
            $type=intval($_GET['type']);
        }else{
            $type="";
        }
        $this->assign("place",$place);
        $this->assign("type",$type);
        $sort=D("CapitalSort");
        $where=array(
            "mid"=>  $this->uid,
            "fsid"=>array("neq",0),
            "place"=>array("in",array("1","0"))
        );
        $list['sort']=$sort->where($where)->select();
        $where['place']=array("in",array("2"));
        $list['exp']=$sort->where($where)->select();
        $where['place']=array("in",array("0"));
        $list['c_sort']=$sort->where($where)->select();
        $where['place']=array("in",array("1"));
        $list['d_sort']=$sort->where($where)->select();
        $this->assign("list",$list);
        
        //查询账户列表
        $member=D("CapitalSortMember");
        $where=Array(
            "mid"=>  $this->uid
        );
        $member_list=$member->where($where)->select();
        $this->assign("member",$member_list);
        
        
        
        $this->display();
    }
    
    public function doadd(){
        if(isset($_GET['place'])&& intval($_GET['place'])>0
                &&isset($_POST['in_sid'])){
            $place=intval($_GET['place']);
            $time=  strtotime($_POST['time']);
            
            $insert_arr=Array(
                "mid"=>  $this->uid,
                "in_sid"=>$_POST['in_sid'],
                "exp_sid"=>$_POST['exp_sid'],
                "place"=>$place,
                "number"=>$_POST['number'],
                "content"=>$_POST['content'],
                "creattime"=>time(),
                "time"=>$time,
                "smid"=>$_POST['smid']
            );
            $sort=D("Capital");
            $capitalsort=D("CapitalSort");
            //查询原始资料
            $old_in=$capitalsort->where(array("sid"=>$_POST['in_sid']))->find();
            
            
            $sort->startTrans();
            $insert=$sort->data($insert_arr)->add();
            if($insert){
                $where=Array(
                    "sid"=>$_POST['in_sid']
                );
                $in_sort=$capitalsort->where($where)->find();
                //转入账户资金变化
                if($in_sort['place']==="0" && $place==2){
                    //转入账户为资产
                    $update_capital_arr=Array(
                        "in_old"=>$old_in['number'],
                        "in_new"=>$old_in['number']+$_POST['number'],
                    );
                }elseif($in_sort['place']==1 && $place==2){
                    //转入账户为债务
                    $update_capital_arr=Array(
                        "in_old"=>$old_in['number'],
                        "in_new"=>$old_in['number']-$_POST['number']
                    );
                }elseif($place==1 || $place==3){
                    //增加或消费时
                    $update_capital_arr=Array(
                        "in_old"=>$old_in['number'],
                        "in_new"=>$old_in['number']+$_POST['number'],
                    );
                }
                $update=$capitalsort->where($where)->data(array("number"=>$update_capital_arr['in_new']))->save();
                if($update){
                    $old_exp=$capitalsort->where(array("sid"=>$_POST['exp_sid']))->find();
                    $update=$sort->where(array("id"=>$insert))->data($update_capital_arr)->save();
                    if($update){
                        unset($update_capital_arr);
                        if(intval($_POST['exp_sid'])>0){
                            $where=Array(
                                "sid"=>$_POST['exp_sid']
                            );
                            $exp_sort=$capitalsort->where($where)->find();
                            //转出账户资金变化
                            //转账时
                            if($place==2){
                                if($exp_sort['place']==1){
                                    //转出账户为债务
                                    $update_capital_arr=Array(
                                        "exp_old"=>$old_exp['number'],
                                        "exp_new"=>$old_exp['number']+$_POST['number']
                                    );
                                }elseif($exp_sort['place']==='0'){
                                    //转出账户为资本
                                    $update_capital_arr=Array(
                                        "exp_old"=>$old_exp['number'],
                                        "exp_new"=>$old_exp['number']-$_POST['number']
                                    );
                                }
                                //消费时
                            }elseif($place=3){
                                //转出账户为债务时
                                if($exp_sort['place']==1){
                                    $update_capital_arr=Array(
                                        "exp_old"=>$old_exp['number'],
                                        "exp_new"=>$old_exp['number']+$_POST['number']
                                    );
                                }elseif($exp_sort['place']==="0"){
                                    //转出账户为资产时
                                    $update_capital_arr=Array(
                                        "exp_old"=>$old_exp['number'],
                                        "exp_new"=>$old_exp['number']-$_POST['number']
                                    );
                                }
                            }
                            $update=$capitalsort->where($where)->data(array("number"=>$update_capital_arr['exp_new']))->save();
                            if($update){
                                $update=$sort->where(array("id"=>$insert))->data($update_capital_arr)->save();
                                if($update){
                                    $sort->commit();
                                }else{
                                  $sort->rollback();
                                    $this->error("数据添加失败");  
                                }
                            }else{
                                $sort->rollback();
                                $this->error("数据添加失败");
                            }
                        }else{
                            $sort->commit();

                        }
                    }else{
                        echo $sort->getLastSql();
                        $sort->rollback();
                    }
                }else{
                    echo $capitalsort->getLastSql();
                    $sort->rollback();
                    //$this->error("in_添加失败");
                }
                $this->redirect("index");
            }else{
                $sort->rollback();
                $this->error("添加记录失败");
            }
        }
    }
    public function edit(){
        if(isset($_GET['id'])&&  intval($_GET['id'])>0){
            $id=intval($_GET['id']);
            $capital=D("Capital");
            $where=Array(
                C("DB_PREFIX")."capital.id"=>$id,
                C("DB_PREFIX")."capital.mid"=>  $this->uid
            );
            $capitals=$capital
                    ->where($where)
                    ->join(C("DB_PREFIX")."capital_sort in_sort ON in_sort.sid=".C("DB_PREFIX")."capital.in_sid")
                    ->join(C("DB_PREFIX")."capital_sort exp_sort ON exp_sort.sid=".C("DB_PREFIX")."capital.exp_sid")
                    ->field(C("DB_PREFIX")."capital.*,in_sort.place as in_place,in_sort.name as in_name,exp_sort.place as exp_place,exp_sort.name as exp_name")
                    ->find();
            $this->assign("vo",$capitals);
            $this->assign("place",$capitals['place']);
            
            //查询账户列表
            $member=D("CapitalSortMember");
            $where=Array(
                "mid"=>  $this->uid
            );
            $member_list=$member->where($where)->select();
            $this->assign("member",$member_list);
        
            
            $this->display();
        }  else {
            $this->error("数据错误");
        }
    }
    public function doedit(){
        if(isset($_POST['time'])&&  $_POST['time']!=''
                &&isset($_GET['id'])&&  intval($_GET['id'])>0){
            $id=  intval($_GET['id']);
            $time=strtotime($_POST['time']);
            $update_arr=Array(
                "content"=>$_POST['content'],
                "time"=>$time,
                "smid"=>$_POST['smid']
            );
            $where=Array(
                "mid"=>  $this->uid,
                "id"=>$id
            );
            $capital=D("Capital");
            $update=$capital->where($where)->data($update_arr)->save();
            
            if($update!==false){
                $this->redirect("index");
            }else{
                $this->error("修改失败");
            }
        }else{
            $this->error("数据错误");
        }
    }
    
    public function del(){
        if(isset($_GET['id'])&&  intval($_GET['id'])>0){
            $id=  intval($_GET['id']);
            $capital=D("Capital");
            $sort=D("CapitalSort");
            $capitals=$capital
                    ->where(array(C("DB_PREFIX")."capital.id"=>$id))
                    ->join(C("DB_PREFIX")."capital_sort in_sort ON in_sort.sid=".C("DB_PREFIX")."capital.in_sid")
                    ->join(C("DB_PREFIX")."capital_sort exp_sort ON exp_sort.sid=".C("DB_PREFIX")."capital.exp_sid")
                    ->field(C("DB_PREFIX")."capital.*,in_sort.place as in_place,exp_sort.place as exp_place")
                    ->find();
            $place=$capitals['place'];
            $in_place=$capitals['in_place'];
            $exp_place=$capitals['exp_place'];
            $capital->startTrans();
            $del=$capital->where(array("id"=>$id))->delete();
            if($del){
                if($place==1){
                    //删除增加数据时
                    $dec=$sort->where(array("sid"=>$capitals['in_sid']))->setDec("number",$capitals['number']);
                    if($dec){
                        $capital->commit();
                        $this->success("删除成功");
                    }else{
                        $capital->rollback();
                        $this->error("删除失败");
                    }
                }elseif($place==2){
                    //删除转账记录时
                    if($in_place==="0"){
                        $dec=$sort->where(array("sid"=>$capitals['in_sid']))->setDec("number",$capitals['number']);
                        if($dec){
                            if($exp_place==="0"){
                                $exp=$sort->where(array("sid"=>$capitals['exp_sid']))->setInc("number",$capitals['number']);
                            }elseif($exp_place==="1"){
                                $exp=$sort->where(array("sid"=>$capitals['exp_sid']))->setDec("number",$capitals['number']);
                            }
                            if($exp){
                                $capital->commit();
                                $this->success("删除成功");
                            }else{
                                $capital->rollback();
                                $this->error("删除失败");
                            }
                        }else{
                            $capital->rollback();
                            $this->error("删除失败");
                        }
                    }elseif($in_place==="1"){
                        if($exp_place==="0"){
                            $in=$sort->where(array("sid"=>$capitals['in_sid']))->setInc("number",$capitals['number']);
                        }
                        if($in){
                            $exp=$sort->where(array("sid"=>$capitals['exp_sid']))->setInc("number",$capitals['number']);
                            if($exp){
                                $capital->commit();
                                $this->success("删除成功");
                            }else{
                                $capital->rollback();
                                $this->error("删除失败");
                            }
                        }else{
                            $capital->rollback();
                            $this->error("删除失败");
                        }
                    }
                }elseif($place==3){
                    //删除消费记录时
                    $dec=$sort->where(array("sid"=>$capitals['in_sid']))->setDec("number",$capitals['number']);
                    if($dec){
                        if($exp_place==="0"){
                            $exp=$sort->where(array("sid"=>$capitals['exp_sid']))->setInc("number",$capitals['number']);
                        }elseif($exp_place==="1"){
                            $exp=$sort->where(array("sid"=>$capitals['exp_sid']))->setDec("number",$capitals['number']);
                        }
                        if($exp){
                            $capital->commit();
                            $this->success("删除成功");
                        }else{
                            $capital->rollback();
                            $this->error("删除失败");
                        }
                    }else{
                        $capital->rollback();
                        $this->error("删除失败");
                    }
                }
             }else{
                $capital->rollback();
                $this->error("删除失败");
             }
        }else{
            $this->error("数据错误");
        }
    }
    
}